// pages/orderDetail/index.js
const { baseUrl } = require('../../globalData')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderData: {},
        goodsList: []
    },
    // 订单数据初始化
    orderDetailInit(id) {
        const token = wx.getStorageSync('token');
        wx.request({
            url: baseUrl + `/wxorderDetail/${id}`,
            header: {
                'content-type': 'application/json',
                token
            },
            method: 'GET',
            success: (res) => {
                const { data: rel } = res
                if (rel.code === 20000) {
                    this.setData({
                        orderData: rel.data.orderData,
                        goodsList: rel.data.listData
                    })
                }
            }
        });
    },
    // 确认收货
    confirmOrder() {
        const token = wx.getStorageSync('token');
        const orderId = this.data.orderData.id
        wx.request({
            url: baseUrl + `/wxconfirmOrder/${orderId}`,
            header: {
                'content-type': 'application/json',
                token
            },
            method: 'PUT',
            success: (res) => {
                const { data: rel } = res
                if (rel.code === 20000) {
                    wx.redirectTo({
                        url: '/pages/orderList/index?index=1',
                    })
                    wx.showToast({
                        title: '已确认收货',
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        });
    },
    // 重新支付订单
    payOrder() {
        const token = wx.getStorageSync('token');
        const orderId = this.data.orderData.id
        wx.request({
            url: baseUrl + `/wxpayOrder/${orderId}`,
            header: {
                'content-type': 'application/json',
                token
            },
            method: 'PUT',
            success: (res) => {
                const { data: rel } = res
                if (rel.code === 20000) {
                    wx.redirectTo({
                        url: '/pages/orderList/index?index=3',
                    })
                    wx.showToast({
                        title: '支付成功',
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        });
    },
    // 取消订单
    cancelOrder() {
        const token = wx.getStorageSync('token');
        const orderId = this.data.orderData.id
        wx.request({
            url: baseUrl + `/wxdeleteOrder/${orderId}`,
            header: {
                'content-type': 'application/json',
                token
            },
            method: 'DELETE',
            success: (res) => {
                const { data: rel } = res
                if (rel.code === 20000) {
                    wx.redirectTo({
                        url: '/pages/orderList/index?index=1',
                    })
                    wx.showToast({
                        title: '删除成功',
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let orderId = options.id
        this.orderDetailInit(orderId)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})